<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class BimbinganControllerSima extends MX_Controller {

    private $_template = "sima_iainmataram";

    function __construct()
	{
		parent::__construct();
        $this->access_library_sima->is_logged_in();
        $this->load->model('bimbingan_model');
        $this->load->model('siam_akademik/akad_model');
	}

    function viewMahasiswaPA()
	{
	    $angkatan_termuda = $this->system_model->getSubstringNimLimit1($this->session->userdata('prodi_unp'));
        $angkatan_termuda = ($angkatan_termuda==NULL)? NULL:$angkatan_termuda->ANGKATAN;

        $data['angkatan'] = $this->system_model->getSubstringNimProdi($this->session->userdata('prodi_unp'));
        $data['page'] = 'filter_lihat_pa';
        $this->load->view($this->_template,$data);
     // $this->load->view('filter_lihat_pa');
	}

    function viewMahasiswaPA2()
	{
      $data['id_angkatan'] = $this->input->post('id_angkatan');
      $this->load->view('lihat_pa_ajax',$data);
	}

    function getDataku(){
      $json = $this->bimbingan_model->get($this->session->userdata('simaunp_user'));
      $results = $this->bimbingan_model->countMahasiswa($this->session->userdata('simaunp_user'));
      //echo "[data:" .json_encode($json)."]";
      echo '{success:true, message:"Loaded data" ,results:'. $results .
                  ', data:'.json_encode($json).'}';
    }

	function viewMahasiswaPAext($id_angkatan=0)
	{
  		$start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
		$limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 15;

        //echo $angkatan;exit();
		$cnt = $this->bimbingan_model->countMahasiswa($this->session->userdata('simaunp_user'),$id_angkatan);

        if($cnt > 0)
        {
          $kontrol = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
          $info_semester = $kontrol->SEMESTER;
          $info_tahun = $kontrol->TAHUN_AJARAN;

   		  $data = $this->bimbingan_model->get($this->session->userdata('simaunp_user'),$id_angkatan,$start,$limit);

          foreach ($data as $obj)
          {
            //$arr[] = $obj;
            $obj_plus = Array(
                         'NAMA' => $obj->NAMA,
                         'NIM' => $obj->NIM,
                         'JENIS_KELAMIN' => $obj->JENIS_KELAMIN,
                         'STATUS' => $obj->STATUS,
                        );
            $arr[] = $obj_plus;
          }
         // echo '({"total":"'.$cnt.'", "results":'.$data.'})';
          echo '{success:true, message:"Loaded data" ,results:'. $cnt .
                  ', data:'.json_encode($data).'}';
        }
        else
        {
          echo '{success:false, message:"Data tidak tersedia" ,results:'. $cnt .
                  ', data:[]}';
        }

       //$this->load->view('lihat_pa',$data);
	}

    public function get($nip,$start,$limit)
	{
		$start = 0;
		$limit = 10;

		if ($this->input->post('start') && $this->input->post('limit'))
		{
			$start = $this->input->post('start');
			$limit = $this->input->post('limit');
		}
		$cnt = $this->phonebook->count();
		if ($cnt > 0)
		{
			$data = $this->phonebook->get($start, $limit);
			echo '({"total":"'.$cnt.'", "results":'.$data.'})';
		}
		else
		{
			echo '({"total":"0", "results":""})';
		}

	}

	function viewDetail($nim)
	{
		$data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
        if($nim)
        {
		    if($data['kontrol']->KODE_SEMESTER ==1){
              $kode_semester = 2;
              $tahun = $data['kontrol']->KODE_TAHUN  - 1;
            }
            else if($data['kontrol']->KODE_SEMESTER ==2){
              $kode_semester = 1;
              $tahun = $data['kontrol']->KODE_TAHUN;
            }
		  $data['biodata'] = $this->bimbingan_model->getDetailMahasiswa($this->session->userdata('simaunp_user'),$nim);
		  $data['getDataKhs'] = $this->bimbingan_model->getViewKhsAll($nim,$tahun,$kode_semester);
          $data['getDataIpk'] = $this->bimbingan_model->getViewKhsAllByNim($nim);
		  $data['optionsSemester'] = $this->system_model->getSelectOptionsValueSemester();
          $data['optionsTahun'] = $this->system_model->getSelectOptionsValueTahunAjaran();
          //$data['getDataKrs'] = $this->bimbingan_model->getViewKrsTemp($nim);
		  $data['getDataMhs'] = $this->bimbingan_model->getDetailMahasiswa($this->session->userdata('simaunp_user'),$nim);
          $data['info_semester'] = $data['kontrol']->SEMESTER;
          $data['info_tahun'] = $data['kontrol']->TAHUN_AJARAN;
		  $data['page'] = 'lihat_biodata';
        }
        else
        {
          $data['page'] = 'lihat_pa';
        }
        $this->load->view($this->_template,$data);
	}

	function viewMahasiswaKkn()
	{
       $angkatan_termuda = $this->system_model->getSubstringNimLimit1($this->session->userdata('prodi_unp'));
        $angkatan_termuda = ($angkatan_termuda==NULL)? NULL:$angkatan_termuda->ANGKATAN;

        $data['angkatan'] = $this->system_model->getSubstringNimProdi($this->session->userdata('prodi_unp'));
        $data['page'] = 'filter_lihat_kkn';
       $this->load->view($this->_template,$data);
	}

    function viewMahasiswaKkn2()
	{
      $data['id_angkatan'] = $this->input->post('id_angkatan');
      $this->load->view('lihat_kkn_edel2',$data);
	}

    function viewMahasiswaKKNext($id_angkatan=0)
	{
  		$start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
		$limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 15;

        //echo $angkatan;exit();
		$cnt = $this->bimbingan_model->countMahasiswaKkn($this->session->userdata('simaunp_user'),$id_angkatan);

        if($cnt > 0)
        {
          $kontrol = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
          $info_semester = $kontrol->SEMESTER;
          $info_tahun = $kontrol->TAHUN_AJARAN;

   		  $data = $this->bimbingan_model->getKkn($this->session->userdata('simaunp_user'),$id_angkatan,$start,$limit);

          foreach ($data as $obj)
          {
            //$arr[] = $obj;
            $obj_plus = Array(
                         'NAMA' => $obj->NAMA,
                         'NIM' => $obj->NIM,
                         'PRODI' => $obj->PRODI,
                         'TEMPAT_KKN' => $obj->TEMPAT_KKN,
                        );
            $arr[] = $obj_plus;
          }
         // echo '({"total":"'.$cnt.'", "results":'.$data.'})';
          echo '{success:true, message:"Loaded data" ,results:'. $cnt .
                  ', data:'.json_encode($data).'}';
        }
        else
        {
          echo '{success:false, message:"Data tidak tersedia" ,results:'. $cnt .
                  ', data:[]}';
        }

       //$this->load->view('lihat_pa',$data);
	}

	function viewDetailKkn($nim)
	{
		$data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
        if($nim)
        {
		    if($data['kontrol']->KODE_SEMESTER ==1){
              $kode_semester = 2;
              $tahun = $data['kontrol']->KODE_TAHUN  - 1;
            }
            else if($data['kontrol']->KODE_SEMESTER ==2){
              $kode_semester = 1;
              $tahun = $data['kontrol']->KODE_TAHUN;
            }
		  $data['biodata'] = $this->bimbingan_model->getDetailMahasiswaKknPplSkripsi($nim);
		  $data['getDataKhs'] = $this->bimbingan_model->getViewKhsAll($nim,$tahun,$kode_semester);
          $data['getDataIpk'] = $this->bimbingan_model->getViewKhsAllByNim($nim);
          $data['getDataKkn'] = $this->bimbingan_model->getDetailMahasiswaKkn($this->session->userdata('simaunp_user'),$nim);
		  $data['getDataMhs'] = $this->bimbingan_model->getDetailMahasiswa($this->session->userdata('simaunp_user'),$nim);
          $data['info_semester'] = $data['kontrol']->SEMESTER;
          $data['info_tahun'] = $data['kontrol']->TAHUN_AJARAN;
		  $data['page'] = 'lihat_detail_kkn';
        }
        else
        {
          $data['page'] = 'lihat_kkn';
        }
        $this->load->view($this->_template,$data);
	}

	function viewMahasiswaPpl()
	{
       $angkatan_termuda = $this->system_model->getSubstringNimLimit1($this->session->userdata('prodi_unp'));
        $angkatan_termuda = ($angkatan_termuda==NULL)? NULL:$angkatan_termuda->ANGKATAN;

        $data['angkatan'] = $this->system_model->getSubstringNimProdi($this->session->userdata('prodi_unp'));
        $data['page'] = 'filter_lihat_ppl';
       $this->load->view($this->_template,$data);
	}

    function viewMahasiswaPpl2()
	{
      $data['id_angkatan'] = $this->input->post('id_angkatan');
      $this->load->view('lihat_ppl_edel2',$data);
	}

    function viewMahasiswaPPLext($id_angkatan=0)
	{
  		$start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
		$limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 15;

        //echo $angkatan;exit();
		$cnt = $this->bimbingan_model->countMahasiswaPpl($this->session->userdata('simaunp_user'),$id_angkatan);

        if($cnt > 0)
        {
          $kontrol = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
          $info_semester = $kontrol->SEMESTER;
          $info_tahun = $kontrol->TAHUN_AJARAN;

   		  $data = $this->bimbingan_model->getPpl($this->session->userdata('simaunp_user'),$id_angkatan,$start,$limit);

          foreach ($data as $obj)
          {
            //$arr[] = $obj;
            $obj_plus = Array(
                         'NAMA' => $obj->NAMA,
                         'NIM' => $obj->NIM,
                         'PRODI' => $obj->PRODI,
                         'TEMPAT_PPL' => $obj->TEMPAT_PPL,
                        );
            $arr[] = $obj_plus;
          }
         // echo '({"total":"'.$cnt.'", "results":'.$data.'})';
          echo '{success:true, message:"Loaded data" ,results:'. $cnt .
                  ', data:'.json_encode($data).'}';
        }
        else
        {
          echo '{success:false, message:"Data tidak tersedia" ,results:'. $cnt .
                  ', data:[]}';
        }

       //$this->load->view('lihat_pa',$data);
	}

	function viewDetailPpl($nim)
	{
		$data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
        if($nim)
        {
		    if($data['kontrol']->KODE_SEMESTER ==1){
              $kode_semester = 2;
              $tahun = $data['kontrol']->KODE_TAHUN  - 1;
            }
            else if($data['kontrol']->KODE_SEMESTER ==2){
              $kode_semester = 1;
              $tahun = $data['kontrol']->KODE_TAHUN;
            }
		  $data['biodata'] = $this->bimbingan_model->getDetailMahasiswaKknPplSkripsi($nim);
		  $data['getDataKhs'] = $this->bimbingan_model->getViewKhsAll($nim,$tahun,$kode_semester);
          $data['getDataIpk'] = $this->bimbingan_model->getViewKhsAllByNim($nim);
          $data['getDataPpl'] = $this->bimbingan_model->getDetailMahasiswaPpl($this->session->userdata('simaunp_user'),$nim);
		  $data['getDataMhs'] = $this->bimbingan_model->getDetailMahasiswa($this->session->userdata('simaunp_user'),$nim);
          $data['info_semester'] = $data['kontrol']->SEMESTER;
          $data['info_tahun'] = $data['kontrol']->TAHUN_AJARAN;
		  $data['page'] = 'lihat_detail_ppl';
        }
        else
        {
          $data['page'] = 'lihat_ppl';
        }
        $this->load->view($this->_template,$data);
	}
	
	function viewMahasiswaSkripsi()
	{
       $data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
       $data['mhsskripsi'] = $this->bimbingan_model->getMahasiswaSkripsi($this->session->userdata('simaunp_user'));
       $data['page'] = 'lihat_skripsi';
       $this->load->view($this->_template,$data);
	}
	
	function viewDetailSkripsi($nim)
	{
		$data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
        if($nim)
        {
		    if($data['kontrol']->KODE_SEMESTER ==1){
              $kode_semester = 2;
              $tahun = $data['kontrol']->KODE_TAHUN  - 1;
            }
            else if($data['kontrol']->KODE_SEMESTER ==2){
              $kode_semester = 1;
              $tahun = $data['kontrol']->KODE_TAHUN;
            }
		  $data['biodata'] = $this->bimbingan_model->getDetailMahasiswa($this->session->userdata('simaunp_user'),$nim);
		  $data['getDataKhs'] = $this->bimbingan_model->getViewKhsAll($nim,$tahun,$kode_semester);
          $data['getDataIpk'] = $this->bimbingan_model->getViewKhsAllByNim($nim);
          $data['getDataSkripsi'] = $this->bimbingan_model->getDetailMahasiswaSkripsi($this->session->userdata('simaunp_user'),$nim);
		  $data['getDataMhs'] = $this->bimbingan_model->getDetailMahasiswa($this->session->userdata('simaunp_user'),$nim);
          $data['info_semester'] = $data['kontrol']->SEMESTER;
          $data['info_tahun'] = $data['kontrol']->TAHUN_AJARAN;
		  $data['page'] = 'lihat_detail_skripsi';
        }
        else
        {
          $data['page'] = 'lihat_skripsi';
        }
        $this->load->view($this->_template,$data);
	}

	function cek_data($nim)
	{
		$data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
		$validasi = $this->validasi_model->cekValidasi($this->input->post('nim'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);
		$data['getDataKrs'] = $this->validasi_model->getViewKrsTemp($this->input->post('nim'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);
		
		if($validasi==NULL)
	      {
		  	foreach($data['getDataKrs'] as $krs):
            $this->validasi_model->insertDataValidasi($this->input->post('nim'),$krs->ID);
			endforeach;
			$this->validasi_model->updateDataValidasi($this->input->post('nim'));
            $status = 1;
		  }
          else
          {
            $status = 2;
          }
        return $status;
	}

    function prosesValidasi()
	{
		$data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));
        $status = $this->cek_data($this->input->post('nim'));

        if($this->input->post('key') != 1)
          $status = 0;

        switch ($status)
        {
          case 1:
			
             $data['message'] = 'Data telah disimpan. Simpan data akun Anda dengan baik.';
             $data['clr'] = 'p_messages_success';
             break;

          case 2:

             $data['message'] = 'Data gagal disimpan, data telah disetujui.';
             $data['clr'] = 'p_messages_error';
             break;

          default:

             $data['message'] = NULL;
             break;
        }

        $data['page'] = 'lihat_krs';
        //$this->load->view($this->_template,$data);
		redirect('sima/approval.html', 'refresh');
		
	}
	
    function prosesEntriKrs()
	{
        $mhs = $this->akad_model->getDataMahasiswa($this->session->userdata('siamunp_user'));

        if($mhs != NULL)
        {

          $data['kontrol'] = $this->system_model->getViewKontrolData($mhs->KODE_PRODI);

          $validasi = $this->akad_model->cekValidasi($this->session->userdata('siamunp_user'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);

          $mahasiswa = $this->akad_model->getMahasiswa($this->session->userdata('siamunp_user'));

          if($data['kontrol']->KODE_STATUS !=1)
          {
            $data['error'] = "Error: Saat ini diluar masa entri KRS.";
            $data['page'] = 'isi_krs_error';
          }
          else if($mahasiswa->STATUS == 0)
          {
            $data['error'] = "Error: Anda tidak bisa melakukan pengisian KRS karena belum melakukan registrasi.";
            $data['page'] = 'isi_krs_error';
          }
          else if($validasi)
          {
             $data['error'] = "Error: Anda tidak bisa melakukan pengisian KRS karena KRS telah divalidasi oleh Dosen Pembimbing Akademik.";
             $data['page'] = 'isi_krs_error';

             $this->load->view($this->_template,$data);
          }
          else if($this->input->post('nim') == NULL)
          {
            if($data['kontrol']->KODE_SEMESTER ==1){

              $kode_semester = 2;

              $tahun = $data['kontrol']->KODE_TAHUN  - 1;
            }
            else if($data['kontrol']->KODE_SEMESTER ==2){

              $kode_semester = 1;

              $tahun = $data['kontrol']->KODE_TAHUN;
            }

            $data['getDataKhs'] = $this->akad_model->getViewKhsAll($this->session->userdata('siamunp_user'),$tahun,$kode_semester);

            $data['krs'] = $this->akad_model->getViewKrsTemp($this->session->userdata('siamunp_user'),$data['kontrol']->KODE_SEMESTER,$data['kontrol']->KODE_TAHUN);

            $data['matakuliahBaru'] = $this->akad_model->dataMatakuliahTersediaById($this->input->post('mk'));

            $data['page'] = 'proses_krs';

          }
          else
          {
            if($this->input->post('tersedia') != "0000")
            {
              $this->akad_model->insertDataKrs($this->input->post('tersedia'), $this->input->post('nim'));
            }

            redirect('siam/entri-krs.html', 'refresh');
          }

        }
        else if($mhs == NULL)
        {
          $data['kontrol'] = NULL;
          $data['error'] = "Error: Maaf, data tidak dapat ditampilkan. Mungkin ada gangguan teknis. Silakan menghubungi bagian terkait.";
          $data['page'] = 'isi_krs_error';
        }

        $this->load->view($this->_template,$data);
	}
	
	function viewJadwalKuliah()
	{
        $data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));

        $id_hari = $this->uri->segment(3);

        if ( ($id_hari == NULL) || ($id_hari > 5) )
           $id_hari = 1;

        //if ( ($data['mhs'] != NULL) && ($data['kontrol'] != NULL) )
           $data['jadwalKuliah'] = $this->akad_model->getJadwalKuliah($this->session->userdata('siamunp_user_prodi'), $data['kontrol']->KODE_SEMESTER, $data['kontrol']->KODE_TAHUN,$id_hari);
        //else
           //$data['jadwalKuliah'] = NULL;

        $data['page'] = 'lihat_jadwal_kuliah';

        $this->load->view($this->_template,$data);
	}

    function viewJadwalUjian()
	{
        $data['kontrol'] = $this->system_model->getViewKontrolData($this->session->userdata('prodi_unp'));

        $data['jadwalUjian'] = $this->akad_model->getJadwalUjian($this->session->userdata('siamunp_user_prodi'), $data['kontrol']->KODE_SEMESTER, $data['kontrol']->KODE_TAHUN);

        $data['page'] = 'lihat_jadwal_ujian';

        $this->load->view($this->_template,$data);
	}

    function viewKhs()
	{
	    if($this->input->post('semester') && $this->input->post('tahun'))
        {
          $cari = $this->akad_model->getViewKhsAll($this->input->post('nim'),$this->input->post('tahun'),$this->input->post('semester'));
          if($cari)
          $data['getDataKhs'] = $this->akad_model->getViewKhsAll($this->input->post('nim'),$this->input->post('tahun'),$this->input->post('semester'));
          else
          $data['getDataKhs'] = $this->akad_model->getViewKhsAllKonversi($this->input->post('nim'),$this->input->post('tahun'),$this->input->post('semester'));

          $data['getDataIpk'] = $this->bimbingan_model->getViewKhsAllByNim($this->input->post('nim'));

          $data['page'] = 'detail_khs';
        }
       
        $this->load->view($this->_template,$data);
	}

    function viewTranskrip()
	{
	    $data['getDataTranskrip'] = $this->akad_model->getViewKhsAllByNim($this->session->userdata('siamunp_user'));

        $data['page'] = 'transkrip_khs';

        $this->load->view($this->_template,$data);
	}

    function viewPresensi()
	{
        $data['mhs'] = $this->akad_model->getDataMahasiswa($this->session->userdata('siamunp_user'));

        if ( ($data['mhs'] != NULL)){

          $data['kontrol'] = $this->system_model->getViewKontrolData($data['mhs']->KODE_PRODI);

          $data['daftarMkPresensi'] = $this->akad_model->getDaftarMkPresensi($data['kontrol']->KODE_SEMESTER, $data['kontrol']->KODE_TAHUN, $this->session->userdata('siamunp_user'));


        }
        else{

           $data['daftarMkPresensi'] = NULL;

           $data['mhs'] = NULL;

        }

        $data['page'] = 'lihat_presensi';

        $this->load->view($this->_template,$data);
	}

    function detailPresensi()
	{
	    $data['presensiData'] = $this->akad_model->getPresensiData($this->input->post('tahun'),$this->input->post('semester'),$this->input->post('mk'));

        $data['presensiDataNim'] = $this->akad_model->getPresensiDataNim($this->session->userdata('siamunp_user'),$this->input->post('tahun'),$this->input->post('semester'),$this->input->post('mk'));

        $data['page'] = 'detail_presensi';

        $this->load->view($this->_template,$data);
	}

    function detailKeuangan()
    {
        $data['dataNeraca'] = $this->akad_model->getNeraca($this->session->userdata('siamunp_user'));
    }

}

